#include "equipment.h"
#define LABELWIDTH 100
#define LABELHIGHTH 30
#define AVGHEIGHT 30

Equipment::Equipment(QWidget *parent) :
    QWidget(parent)
{
    QFont ft;
    ft.setPixelSize(20);
    tabWidget=new QTabWidget();
    tabWidget->setFixedSize(900,450);
    QString tabBarStyle = "QTabBar::tab {min-width:100px;color:rgb(250,255,150);background-color:qlineargradient(x1:0, y1:0, x2:0, y2:1, stop: 0 rgb(180,183,188), stop: 1 gray);border: 0px solid;border-top-left-radius: 10px;border-top-right-radius: 10px;padding:5px;}QTabBar::tab:!selected {margin-top: 5px;}QTabBar::tab:selected {background-color:darkblue;color: yellow;}";
    tabWidget->setStyleSheet(tabBarStyle);
    tabWidget->setFont(ft);
//*********************************设备检定*****************************************//
    //QGroupBox *equipmentbox =new QGroupBox(tr(""));
    Load_box = new QGroupBox(tr(""));//负荷箱校准
    Calibration_box=new QGroupBox(tr(""));//校验仪检定
    internal_box=new QGroupBox(tr(""));//内部标准器检定
    ft.setPixelSize(20);
    calwidget = new QWidget;
    lodwidget = new QWidget;
    tandwidget = new QWidget;

    Load_box_calibration();  //负荷箱校准
    Calibration_verification();//校验仪检定
    Internal_tandard(); //内部标准器检定

    tabWidget->addTab(Load_box,tr("负荷箱校准"));
    tabWidget->addTab(Calibration_box,tr("校验仪检定"));
    tabWidget->addTab(internal_box,tr("内部标准器检定"));

    QHBoxLayout *SLayout = new QHBoxLayout;
    SLayout->addSpacing(30);
    SLayout->addWidget(tabWidget);
    SLayout->addSpacing(30);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(SLayout);
    mainLayout->addStretch(1);
    setLayout(mainLayout);

    theKeyBoard = new Dialog(this);//虚拟键盘

}

void Equipment::Load_box_calibration()
{
    QFont ft;
    ft.setPixelSize(15);
    QLabel *inputI = new QLabel(tr("输入二次电流 :"));
    inputI->setFont(ft);
    QLabel *inputP = new QLabel(tr("输入功率因素 :"));
    inputP->setFont(ft);
    QLabel *inputFH = new QLabel(tr("输入负荷因素 :"));
    inputFH->setFont(ft);
    QLabel *connectR = new QLabel(tr("接线电阻:"));
    connectR->setFont(ft);
    QLabel *danwei1 = new QLabel(tr("A"));
    danwei1->setFont(ft);
    QLabel *danwei2 = new QLabel(tr("VA"));
    danwei2->setFont(ft);
    QLabel *danwei3 = new QLabel(tr("Ω"));
    danwei3->setFont(ft);
//*****************************************************//
    QLabel *eddianzu = new QLabel(tr("额定电阻 :"));
    eddianzu->setFont(ft);
    QLabel *eddiankang = new QLabel(tr("额定电抗 :"));
    eddiankang->setFont(ft);
    QLabel *shicebfd = new QLabel(tr("实测百分点 :"));
    shicebfd->setFont(ft);
    QLabel *shicedianzu = new QLabel(tr("实测电阻 :"));
    shicedianzu->setFont(ft);
    QLabel *shicediankang = new QLabel(tr("实测电抗 :"));
    shicediankang->setFont(ft);
    QLabel *shiceFH = new QLabel(tr("实测负荷 :"));
    shiceFH->setFont(ft);


    QLabel *danwei4 = new QLabel(tr("Ω"));
    danwei4->setFont(ft);
    QLabel *danwei5 = new QLabel(tr("Ω"));
    danwei5->setFont(ft);
    QLabel *danwei6 = new QLabel(tr("Ω"));
    danwei6->setFont(ft);
    QLabel *danwei7 = new QLabel(tr("Ω"));
    danwei7->setFont(ft);
    QLabel *danwei8 = new QLabel(tr("%"));
    danwei8->setFont(ft);
    QLabel *danwei9 = new QLabel(tr("VA"));
    danwei9->setFont(ft);

    Rlabel = new QLabel("-");
    Rlabel->setStyleSheet("color:blue");
    Rlabel->setFont(ft);
    Rlabel->setFixedSize(100,37);
    Rlabel->setAlignment(Qt::AlignCenter);
    Iedit = new QLineEdit;
    Iedit->setFont(ft);
    Iedit->setFixedSize(100,LABELHIGHTH-5);
    Iedit->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");
    Iedit->installEventFilter(this);
    Pedit = new QLineEdit;
    Pedit->setFont(ft);
    Pedit->setFixedSize(100,LABELHIGHTH-5);
    Pedit->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");
    Pedit->installEventFilter(this);
    FHedit = new QLineEdit;
    FHedit->setFont(ft);
    FHedit->setFixedSize(100,LABELHIGHTH-5);
    FHedit->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");
    FHedit->installEventFilter(this);

    eddzEdit = new  QLineEdit;
    eddzEdit->setFont(ft);
    eddzEdit->setFixedSize(100,LABELHIGHTH-5);
    eddzEdit->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");

    eddkEdit = new  QLineEdit;
    eddkEdit->setFont(ft);
    eddkEdit->setFixedSize(100,LABELHIGHTH-5);
    eddkEdit->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");

    scbfdEdit = new  QLineEdit;
    scbfdEdit->setFont(ft);
    scbfdEdit->setFixedSize(100,LABELHIGHTH-5);
    scbfdEdit->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");

    scdzEdit = new  QLineEdit;
    scdzEdit->setFont(ft);
    scdzEdit->setFixedSize(100,LABELHIGHTH-5);
    scdzEdit->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");

    scdkEdit = new  QLineEdit;
    scdkEdit->setFont(ft);
    scdkEdit->setFixedSize(100,LABELHIGHTH-5);
    scdkEdit->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");

    scfhEdit = new  QLineEdit;
    scfhEdit->setFont(ft);
    scfhEdit->setFixedSize(100,LABELHIGHTH-5);
    scfhEdit->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");

    QHBoxLayout *boxlayout1 = new QHBoxLayout;
    boxlayout1->addSpacing(300);
    boxlayout1->addWidget(inputI);
    boxlayout1->addWidget(Iedit);
    boxlayout1->addWidget(danwei1);
    boxlayout1->addStretch(1);

    QHBoxLayout *boxlayout2 = new QHBoxLayout;
    boxlayout2->addSpacing(300);
    boxlayout2->addWidget(inputP);
    boxlayout2->addWidget(Pedit);
    boxlayout2->addStretch(1);

    QHBoxLayout *boxlayout3 = new QHBoxLayout;
    boxlayout3->addSpacing(300);
    boxlayout3->addWidget(inputFH);
    boxlayout3->addWidget(FHedit);
    boxlayout3->addWidget(danwei2);
    boxlayout3->addStretch(1);

//    QHBoxLayout *boxlayout4 = new QHBoxLayout;
//    boxlayout4->addSpacing(600);
//    boxlayout4->addWidget(connectR);
//    boxlayout4->addWidget(Rlabel);
//    boxlayout4->addWidget(danwei3);
//    boxlayout4->addStretch(1);

    QHBoxLayout *boxlayout5 = new QHBoxLayout;
    boxlayout5->addSpacing(330);
    boxlayout5->addWidget(eddianzu);
    boxlayout5->addWidget(eddzEdit);
    boxlayout5->addWidget(danwei4);
    boxlayout5->addSpacing(70);
    boxlayout5->addWidget(connectR);
    boxlayout5->addWidget(Rlabel);
    boxlayout5->addWidget(danwei3);
    boxlayout5->addStretch(1);

    QHBoxLayout *boxlayout6 = new QHBoxLayout;
    boxlayout6->addSpacing(330);
    boxlayout6->addWidget(eddiankang);
    boxlayout6->addWidget(eddkEdit);
    boxlayout6->addWidget(danwei5);
    boxlayout6->addStretch(1);

    QHBoxLayout *boxlayout7 = new QHBoxLayout;
    boxlayout7->addSpacing(315);
    boxlayout7->addWidget(shicebfd);
    boxlayout7->addWidget(scbfdEdit);
    boxlayout7->addWidget(danwei8);
    boxlayout7->addStretch(1);
    QHBoxLayout *boxlayout8 = new QHBoxLayout;
    boxlayout8->addSpacing(330);
    boxlayout8->addWidget(shicedianzu);
    boxlayout8->addWidget(scdzEdit);
    boxlayout8->addWidget(danwei6);
    boxlayout8->addStretch(1);
    QHBoxLayout *boxlayout9 = new QHBoxLayout;
    boxlayout9->addSpacing(330);
    boxlayout9->addWidget(shicediankang);
    boxlayout9->addWidget(scdkEdit);
    boxlayout9->addWidget(danwei7);
    boxlayout9->addStretch(1);
    QHBoxLayout *boxlayout10 = new QHBoxLayout;
    boxlayout10->addSpacing(330);
    boxlayout10->addWidget(shiceFH);
    boxlayout10->addWidget(scfhEdit);
    boxlayout10->addWidget(danwei9);
    boxlayout10->addStretch(1);

    QVBoxLayout *boxmain = new QVBoxLayout;
    boxmain->addStretch(1);
    boxmain->addLayout(boxlayout1);
    boxmain->addSpacing(5);
    boxmain->addLayout(boxlayout2);
    boxmain->addSpacing(5);
    boxmain->addLayout(boxlayout3);
    boxmain->addSpacing(5);
    boxmain->addLayout(boxlayout5);
    boxmain->addSpacing(5);
    boxmain->addLayout(boxlayout6);
    boxmain->addSpacing(30);
    boxmain->addLayout(boxlayout7);
    boxmain->addSpacing(5);
    boxmain->addLayout(boxlayout8);
    boxmain->addSpacing(5);
    boxmain->addLayout(boxlayout9);
    boxmain->addSpacing(5);
    boxmain->addLayout(boxlayout10);
//    boxmain->addSpacing(20);
//    boxmain->addLayout(boxlayout4);
    boxmain->addStretch(1);

    //lodwidget->setLayout(boxmain);
    Load_box->setLayout(boxmain);
}

void Equipment::Calibration_verification()
{
    QFont ft;
    ft.setPixelSize(15);
//    QLabel *inputI = new QLabel(tr("输入二次电流 :"));
//    QLabel *inputP = new QLabel(tr("输入功率因素 :"));
    test1A = new QRadioButton(tr("1A检定"));
    test1A->setFont(ft);
    test1A->setChecked(true);
    test5A = new QRadioButton(tr("5A检定"));
    test5A->setFont(ft);
    grop1 = new QButtonGroup;
    grop1->addButton(test1A);
    grop1->addButton(test5A);
    QLabel *bc = new QLabel(tr("比差      :"));
    bc->setFont(ft);
    QLabel *jc = new QLabel(tr("角差      :"));
    jc->setFont(ft);
    QLabel *bfb = new QLabel(tr("百分表   :"));
    bfb->setFont(ft);
    QLabel *edI = new QLabel(tr("额定电流:"));
    edI->setFont(ft);

    QLabel *danwei1 = new QLabel(tr("%"));
    danwei1->setFont(ft);
    QLabel *danwei2 = new QLabel(tr("‘"));
    danwei2->setFont(ft);
    QLabel *danwei3 = new QLabel(tr("%"));
    danwei3->setFont(ft);
    QLabel *danwei4 = new QLabel(tr("A"));
    danwei4->setFont(ft);

    bcEdit = new QLineEdit;
    bcEdit->setFont(ft);
    bcEdit->setFixedSize(100,LABELHIGHTH-5);
    bcEdit->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");
    bcEdit->installEventFilter(this);

    jcEdit = new QLineEdit;
    jcEdit->setFont(ft);
    jcEdit->setFixedSize(100,LABELHIGHTH-5);
    jcEdit->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");
    jcEdit->installEventFilter(this);

    bfbEdit = new QLineEdit;
    bfbEdit->setFont(ft);
    bfbEdit->setFixedSize(100,LABELHIGHTH-5);
    bfbEdit->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");
    bfbEdit->installEventFilter(this);

    edIEdit = new QLineEdit("1");
    edIEdit->setFont(ft);
    edIEdit->setFixedSize(100,LABELHIGHTH-5);
    edIEdit->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");
    edIEdit->installEventFilter(this);
    range10 = new QRadioButton(tr(" 10% 量程"));
    range10->setFont(ft);
    range10->setChecked(true);
    range1 = new QRadioButton(tr(" 1% 量程"));
    range1->setFont(ft);
    range01 = new QRadioButton(tr(" 0.1% 量程"));
    range01->setFont(ft);
    grop2 = new QButtonGroup;
    grop2->addButton(range10);
    grop2->addButton(range1);
    grop2->addButton(range01);
    QHBoxLayout *calLayout = new QHBoxLayout;
    calLayout->addStretch(1);
    calLayout->addWidget(test1A);
    calLayout->addSpacing(50);
    calLayout->addWidget(test5A);
    calLayout->addStretch(1);

    QHBoxLayout *calLayout1 = new QHBoxLayout;
    //calLayout1->addStretch(1);
    calLayout1->addSpacing(300);
    calLayout1->addWidget(bc);
    calLayout1->addWidget(bcEdit);
    calLayout1->addWidget(danwei1);
    calLayout1->addStretch(1);

    QHBoxLayout *calLayout2 = new QHBoxLayout;
    //calLayout2->addStretch(1);
    calLayout2->addSpacing(300);
    calLayout2->addWidget(jc);
    calLayout2->addWidget(jcEdit);
    calLayout2->addWidget(danwei2);
    calLayout2->addSpacing(70);
    calLayout2->addWidget(range10);
    calLayout2->addStretch(1);


    QHBoxLayout *calLayout3 = new QHBoxLayout;
    //calLayout3->addStretch(1);
    calLayout3->addSpacing(300);
    calLayout3->addWidget(bfb);
    calLayout3->addWidget(bfbEdit);
    calLayout3->addWidget(danwei3);
    calLayout3->addSpacing(60);
    calLayout3->addWidget(range1);
    calLayout3->addStretch(1);


    QHBoxLayout *calLayout4 = new QHBoxLayout;
    //calLayout4->addStretch(1);
    calLayout4->addSpacing(300);
    calLayout4->addWidget(edI);
    calLayout4->addWidget(edIEdit);
    calLayout4->addWidget(danwei4);
    calLayout4->addSpacing(62);
    calLayout4->addWidget(range01);
    calLayout4->addStretch(1);

    QVBoxLayout *mainlay = new QVBoxLayout;
    mainlay->addStretch(1);
    mainlay->addLayout(calLayout);
    mainlay->addSpacing(30);
    mainlay->addLayout(calLayout1);
    mainlay->addSpacing(30);
    mainlay->addLayout(calLayout2);
    mainlay->addSpacing(30);
    mainlay->addLayout(calLayout3);
    mainlay->addSpacing(30);
    mainlay->addLayout(calLayout4);
    mainlay->addStretch(1);

    //calwidget->setLayout(mainlay);
    Calibration_box->setLayout(mainlay);

    connect(test1A,SIGNAL(clicked()),this,SLOT(Set_I()));
    connect(test5A,SIGNAL(clicked()),this,SLOT(Set_I()));

}
void Equipment::Set_I()
{
    if(test1A->isChecked())
    {
        edIEdit->setText("1");
    }
    else if(test5A->isChecked())
    {
        edIEdit->setText("5");
    }
}

void Equipment::Internal_tandard() //内部标准器检定
{
    QFont ft;
    ft.setPixelSize(18);
    tandardlabel = new QLabel(tr("请把一次线接到【5000A~500A】端子上"));
    tandardlabel->setFixedSize(350,50);
    tandardlabel->setFont(ft);
    tandardEdit1 = new QLineEdit(tr("5000A"));
    tandardEdit1->setFont(ft);
    tandardEdit1->setFixedSize(100,LABELHIGHTH-5);
    tandardEdit1->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");
    tandardEdit1->installEventFilter(this);

//    tandardEdit2 = new QLineEdit(tr("5A"));
//    tandardEdit2->setFont(ft);
//    tandardEdit2->setFixedSize(100,LABELHIGHTH-5);
//    tandardEdit2->setStyleSheet("QLineEdit{border-width:0;border-style:outset}");
    tandardcom = new QComboBox;
    tandardcom->setFont(ft);
    tandardcom->setFixedSize(100,LABELHIGHTH-5);
    tandardcom->addItem("1A");
    tandardcom->addItem("5A");


    QLabel *xie = new QLabel(tr("/"));
    xie->setFont(ft);

    ft.setPixelSize(12);
    listwidget1 = new QListWidget;
    listwidget1->setFont(ft);
    listwidget1->setFixedSize(70,320);
    QStringList list;
    list << "5000" << "4000" << "3150" << "3000" << "2500"<<"2000"<<"1800"
         <<"1600"<<"1500"<<"1250"<<"1200"<<"1000"<<"800"<<"750"<<"630"<<"600"
        <<"500"<<"400"<<"315"<<"300"<<"250";
    listwidget1->addItems( list );
    //listwidget1->sortItems();
    QListWidgetItem *item = listwidget1->item(0);//默认选择第0行
    item->setSelected(true);

    listwidget2 = new QListWidget;
    listwidget2->setFont(ft);
    listwidget2->setFixedSize(70,320);
    QStringList list2;
    list2 << "200" << "150" << "125" << "120" << "100"<<"80"<<"75"
         <<"60"<<"50"<<"40"<<"30"<<"25"<<"20"<<"15"<<"12"<<"10"
        <<"8"<<"7.5"<<"6"<<"5";
    listwidget2->addItems( list2 );

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addStretch(1);
    layout->addWidget(listwidget1);
    layout->addSpacing(1);
    layout->addWidget(listwidget2);
    layout->addStretch(1);

    QHBoxLayout *layout1 = new QHBoxLayout;
    layout1->addStretch(1);
    layout1->addWidget(tandardEdit1);
    layout1->addWidget(xie);
    layout1->addWidget(tandardcom);
    layout1->addStretch(1);

    QHBoxLayout *layout2 = new QHBoxLayout;
    layout2->addStretch(1);
    layout2->addWidget(tandardlabel);
    layout2->addStretch(1);

    QVBoxLayout *lay = new QVBoxLayout;
    lay->addSpacing(50);
    lay->addLayout(layout1);
    lay->addSpacing(100);
    lay->addLayout(layout2);
    lay->addStretch(1);
    QVBoxLayout *lay1 = new QVBoxLayout;
    lay1->addLayout(layout);
    lay1->addStretch(1);

    QHBoxLayout *main = new QHBoxLayout;
    //main->addStretch(1);
    main->addSpacing(200);
    main->addLayout(lay);
    main->addSpacing(20);
    main->addLayout(lay1);
    //main->addStretch(1);

    //tandwidget->setLayout(main);
    internal_box->setLayout(main);

    connect(listwidget1,SIGNAL(currentTextChanged(QString)),this,SLOT(List_changed(QString)));
    connect(listwidget2,SIGNAL(currentTextChanged(QString)),this,SLOT(List_changed(QString)));
}
void Equipment::List_changed(QString str)
{
    qDebug()<<"list"<<str;
    float math = str.toFloat();
    qDebug()<<"math"<<math;
    QFont ft;
    ft.setPixelSize(18);

    QString mes ="请把一次线接到【400A~50A】端子上";
    QString mes1 ="请把一次线接到【40A~5A】端子上";
    QString mes2 ="请把一次线接到【5000A~500A】端子上";

    tandardEdit1->setText(str);
    if(math>=500)
    {
        tandardlabel->setText(mes2);
    }
    else if(math<=400 && math>=50)
    {
        tandardlabel->setText(mes);
    }
    else if(math<=40 && math>=5)
    {
        tandardlabel->setText(mes1);
    }

}
bool Equipment::eventFilter(QObject *obj, QEvent *event)
{
    if( obj == Iedit)
    {
        if(event->type() == QEvent::FocusIn)
        {
            theKeyBoard->setCurrentActive(Iedit);
        }
        else if(event->type() == QEvent::FocusOut)
        {
            theKeyBoard->hide();
        }
    }
    if( obj == Pedit)
    {
        if(event->type() == QEvent::FocusIn)
        {
            theKeyBoard->setCurrentActive(Pedit);
        }
        else if(event->type() == QEvent::FocusOut)
        {
            theKeyBoard->hide();
        }
    }
    if( obj == FHedit)
    {
        if(event->type() == QEvent::FocusIn)
        {
            theKeyBoard->setCurrentActive(FHedit);
        }
        else if(event->type() == QEvent::FocusOut)
        {
            theKeyBoard->hide();
        }
    }
    if( obj == bcEdit)
    {
        if(event->type() == QEvent::FocusIn)
        {
            theKeyBoard->setCurrentActive(bcEdit);
        }
        else if(event->type() == QEvent::FocusOut)
        {
            theKeyBoard->hide();
        }
    }
    if( obj == jcEdit)
    {
        if(event->type() == QEvent::FocusIn)
        {
            theKeyBoard->setCurrentActive(jcEdit);
        }
        else if(event->type() == QEvent::FocusOut)
        {
            theKeyBoard->hide();
        }
    }
    if( obj == bfbEdit)
    {
        if(event->type() == QEvent::FocusIn)
        {
            theKeyBoard->setCurrentActive(bfbEdit);
        }
        else if(event->type() == QEvent::FocusOut)
        {
            theKeyBoard->hide();
        }
    }
    if( obj == edIEdit)
    {
        if(event->type() == QEvent::FocusIn)
        {
            theKeyBoard->setCurrentActive(edIEdit);
        }
        else if(event->type() == QEvent::FocusOut)
        {
            theKeyBoard->hide();
        }
    }
    if( obj == tandardEdit1)
    {
        if(event->type() == QEvent::FocusIn)
        {
            theKeyBoard->setCurrentActive(tandardEdit1);
        }
        else if(event->type() == QEvent::FocusOut)
        {
            theKeyBoard->hide();
        }
    }
    return false;
}
